<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="审核状态" prop="verifyState">
        <el-select v-model="queryParams.verifyState" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in verifyStateArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择" clearable @change="handleQuery">
          <el-option v-for="item in stateArr" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['mall:mallSpu:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['mall:mallSpu:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="multiple" @click="handleAuditAll"
          v-hasPermi="['mall:mallSpu:edit']">批量审核</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['mall:mallSpu:remove']">删除</el-button>
      </el-col> -->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mallSpuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="用户名称" align="center" prop="nickname" />
      <el-table-column label="评价星级" align="center" prop="star" />
      <el-table-column label="评价内容" align="center" prop="comment" />
      <el-table-column label="回复内容" align="center" prop="merchantReplyContent" />
      <el-table-column label="评价时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Avatar" @click="handleAudit(scope.row)"
            v-hasPermi="['mall:mallSpu:edit']" v-if="scope.row.merchantReplyContent == ''">回复</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['mall:mallSpu:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <el-dialog title="回复评价" v-model="open" width="500px" append-to-body>
      <el-form ref="forms" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="回复内容" prop="merchantReplyContent">
          <el-input v-model="form.merchantReplyContent" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="MallSpu">
import {
  listMallSpu,
  replyComment,
  delReply,
} from "@/api/mall/mallReply";
import { ref } from "vue";
import { useRouter, useRoute } from "vue-router";

const { proxy } = getCurrentInstance();

const mallSpuList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const router = useRouter();
const route = useRoute();

onMounted(() => {
  route.query.storeId && (queryParams.value.storeId = route.query.storeId);
  route.query.columnType &&
    (queryParams.value.columnType = route.query.columnType);
  getList();
});

const stateArr = ref([
  {
    label: "上架",
    value: 1,
  },
  {
    label: "下架",
    value: 0,
  },
]);

const verifyStateArr = ref([
  {
    label: "未审核",
    value: 0,
  },
  {
    label: "审核通过",
    value: 1,
  },
  {
    label: "审核不通过",
    value: 2,
  },
]);

const columnTypeArr = ref([
  {
    label: "金积分达人",
    value: 1,
  },
  {
    label: "社群达人",
    value: 2,
  },
  {
    label: "本地生活",
    value: 3,
  },
  {
    label: "创业合伙人",
    value: 4,
  },
  {
    label: "消费增值区",
    value: 5,
  },
  {
    label: "熵豆兑换专区",
    value: 6,
  },
  {
    label: "创业联盟区",
    value: 7,
  },
  {
    label: "跨国商贸",
    value: 8,
  },
  {
    label: "",
    value: 9,
  },
  {
    label: "社区数字店",
    value: 10,
  },
  {
    label: "趣味消费节",
    value: 11,
  },
  {
    label: "商品折扣券",
    value: 12,
  },
  {
    label: "资产包",
    value: 13,
  },
]);

const verifyRemarkRule = (rule, value, callback) => {
  if (form.value.verifyState == 0) {
    if (!value) {
      return callback(new Error("请输入审核不通过原因!"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    profile: null,
    detail: null,
    cover: null,
    icons: null,
    type: null,
    storeId: null,
    brand: null,
    orderBy: null,
    state: null,
    isHot: null,
    oldPrice: null,
    price: null,
    verifyState: null,
    verifyBy: null,
    verifyTime: null,
    verifyRemark: null,
    deptId: null,
    isDelete: null,
    columnType: null,
    groupBuy: route.query && route.query.groupBuy,
  },
  form: {},
  rules: {
    merchantReplyContent: {
      required: true,
      message: "请输入回复内容",
      trigger: "blur",
    },
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品列表 */
function getList() {
  loading.value = true;
  listMallSpu(queryParams.value).then((response) => {
    mallSpuList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function cancel() {
  open.value = false;
}

function submitForm() {
  proxy.$refs["forms"].validate((valid) => {
    if (valid) {
      replyComment({ id: form.value.id, merchantReplyContent: form.value.merchantReplyContent },).then((res) => {
        open.value = false;
        getList();
        proxy.$modal.msgSuccess("回复成功");
      });
    }
  });
}


function handleAudit(item) {
  form.value = {
    id: item.id,
    verifyState: 0,
    verifyRemark: "",
    all: false,
  };
  console.log(form.value, "form");
  open.value = true;
}

function handleAuditAll() {
  form.value = {
    id: ids.value,
    verifyState: 0,
    verifyRemark: "",
    all: true,
  };
  open.value = true;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  router.push({
    path: "/mall/commodityAdd",
    query: {
      storeId: queryParams.value.storeId,
      columnType: queryParams.value.columnType,
      groupBuy: route.query && route.query.groupBuy,
    },
  });
}

/** 修改按钮操作 */
function handleUpdate(row) {
  router.push({
    path: "/mall/commodityAdd",
    query: {
      id: row.id,
      storeId: row.storeId,
    },
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delReply(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}
</script>
